package com.example.demo.controller;


import com.example.comon.domain.R;
import com.example.comon.utils.UserUtil;
import com.example.demo.domain.Bo.MenuListBo;
import com.example.demo.domain.Menu;
import com.example.demo.domain.Bo.addMenuBo;
import com.example.demo.domain.Vo.menuListVo;
import com.example.demo.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
@Api(value = "menu",tags = "菜单")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @GetMapping("/getMenuList")
    @ApiOperation("获取菜单列表")
    public R<List<menuListVo>> getMenuList(MenuListBo bo){
        Menu menu = new Menu();
        menu.setId(bo.getId());
        List<menuListVo> list =menuService.selectMenu(menu);
        return R.success(list);
    }

    @PostMapping("getByUserId")
    @ApiOperation("用户有那些菜单")
    public R<List<Menu>> getByUserId( ){
        Integer userId = UserUtil.getLoginUser().getUserId();
        List<Menu> menus = menuService.getByUserId(Long.valueOf(userId));
        return R.success(menus);
    }

@PostMapping("/addMenu")
    @ApiOperation("增加菜单")
public R<Menu> addMenu(@RequestBody addMenuBo bo){
    Menu menu = new Menu();
    menu.setName(bo.getName());
    menu.setRouter(bo.getRouter());
    menu.setFirst_level(bo.getFirst_level());
    menu.setCreate_by(bo.getCreate_by());
    menu.setCreate_time(bo.getCreate_time());
    menu.setLevel(bo.getLevel());
    menu.setUpdate_by(bo.getUpdate_by());
    menu.setUpdate_time(bo.getUpdate_time());

    menuService.insertMenu(menu);
    return R.success(menu);
}

    @PostMapping("/updateMenuById")
    @ApiOperation("修改菜单")
    public R updateMenuById(@RequestBody Menu menu){
        menuService.updateMenuById(menu);
        return R.success();
    }

    @DeleteMapping("/delMenuById")
    @ApiOperation("删除菜单")
    public R delMenuById(String id){
        menuService.delMenuById(id);
        return R.success();
    }


}
